package com.yunchang.springboot.scheduled.async;

/**
 * @Author: lindw
 * @Date: 2020/3/8 15:15
 * @Version 1.0
 */

//@Component
//@Slf4j
public class AsyncTaskServiceImpl {
// extends ReportSchedule implements AsyncTaskService
//    @Autowired
//    AreaMapper areaMapper;
//
//    @Autowired
//    ShopMapper shopMapper;
//
//    @Autowired
//    MwsDataGetErrorRecordMapper mwsDataGetErrorRecordMapper;
//
//    @Async
//    @Override
//    public Future<Object> selDemo(CountDownLatch countDownLatch) {
//        log.info("selDemo异步调用开始");
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        countDownLatch.countDown();
//        return new AsyncResult<>("selDemo成功");
//    }
//
//    @Async
//    @Override
//    public void sleep(CountDownLatch countDownLatch) {
//        log.info("sleep异步调用开始？");
//        try {
//            Thread.sleep(1500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        countDownLatch.countDown();
//        log.info("sleep异步调用结束");
//    }
//
//
//    /**
//     * 当前方法异步执行   填写单线程获取报表的方法
//     *
//     * @param countDownLatch
//     * @return
//     */
//    @Async
//    @Override
//    public void syncSendRequestReport(CountDownLatch countDownLatch, String taskName, Shop shop, Area area) {
//        try {
//            log.info("调用" + taskName + "任务开始！");
//            String sellerId = shop.getSellerId();
//            String mwsAuthToken = shop.getMwsAuthToken();
//            String awsAccessKeyId = shop.getAwsAccessKeyId();
//            String secretKey = shop.getSecretKey();
//            Map<String, String> params = new HashMap<>(7);
//            StringBuffer errorMsg = new StringBuffer();
//            params.put(PARAM_AWS_ACCESS_KEY_ID, AmazonMwsUntil.urlEncode(awsAccessKeyId));
//            params.put(PARAM_MWS_AUTH_TOKEN, AmazonMwsUntil.urlEncode(mwsAuthToken));
//            params.put(PARAM_SELLER_ID, AmazonMwsUntil.urlEncode(sellerId));
//            if (!NOT_MARKETPLACE_ID_LIST_COUNTRY_CODE.contains(area.getMwsCountryCode())) {
//                params.put(PARAM_MARKETPLACE_ID_LIST_ID, AmazonMwsUntil.urlEncode(area.getMarketplaceId()));
//            }
//            params.put("taskName", taskName);
//            params.put("shopName", shop.getShopName());
//            params.put("areaCode", area.getMwsCountryCode());
//            // 发送RequestReport请求
//            Map<String, Map<String, String>> requestReportIdAndParamMap = sendRequestReport(taskName, area, secretKey, params);
//            if (null == requestReportIdAndParamMap) {
//                logWarnErrorMsg(taskName, shop.getShopName(), area.getMwsCountryCode(), errorMsg);
//                errorMsg = new StringBuffer();
//            }
//            if (area.getMwsCountryCode().equals(MX_AREA)) {
//                //墨西哥区域通过GetReportList获取ReportId
//                Map<String, Map<String, String>> reportIdAndParamMapByGetReportList = new HashMap<>();
//                for (String key : requestReportIdAndParamMap.keySet()) {
//                    Map<String, String> paramMap = requestReportIdAndParamMap.get(key);
//                    paramMap.put("ReportRequestId", key);
//                    // 发送GetReportList请求
//                    reportIdAndParamMapByGetReportList = sendGetReportList(paramMap);
//                }
//                if (null != reportIdAndParamMapByGetReportList || reportIdAndParamMapByGetReportList.size() > 0) {
//                    for (String key : reportIdAndParamMapByGetReportList.keySet()) {
//                        Map<String, String> paramMap = reportIdAndParamMapByGetReportList.get(key);
//                        paramMap.put("ReportId", key);
//                        // 发送GetReport请求
//                        sendGetReport(paramMap);
//                    }
//                } else {
//                    log.error("通过GetReportList获取reportId报错，reportIdAndParamMap为空！" + reportIdAndParamMapByGetReportList);
//                    logWarnErrorMsg(taskName, shop.getShopName(), area.getMwsCountryCode(), errorMsg);
//                    errorMsg = new StringBuffer();
//                }
//            } else {
//                // 从通知中获取reportId   返回reportId集合
//                Map<String, Map<String, String>> reportIdAndParamMap = getNotificationSingleton(requestReportIdAndParamMap,errorMsg);
//                if (null != reportIdAndParamMap && reportIdAndParamMap.size() > 0) {
//                    if (!reportIdAndParamMap.containsKey("noData")) {
//                        // 发送getReport请求，并将报告内容插入数据库
//                        for (String key : reportIdAndParamMap.keySet()) {
//                            Map<String, String> paramMap = reportIdAndParamMap.get(key);
//                            paramMap.put("ReportId", key);
//                            sendGetReport(paramMap);
//                        }
//                    }
//                } else {
//                    logWarnErrorMsg(taskName, shop.getShopName(), area.getMwsCountryCode(), errorMsg);
//                    errorMsg = new StringBuffer();
//                }
//            }
//        } catch (Exception e) {
//            log.error("出错了" + e);
//        } finally {
//            log.info("***********获取------" + taskName + "-----定时任务结束**********");
//        }
//        countDownLatch.countDown();
//    }
//
//
//    @Override
//    public void disposeErrorTask() {
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//        String taskEndTime = df.format(new Date());
//        // 错误报告是否全部处理标识
//        Boolean isErrorReport = true;
//        int isErrorReportNum = 0;
//        int isErrorReportMaxNum = 3;
//        while (isErrorReport) {
//            isErrorReportNum++;
//            List<MwsDataGetErrorRecord> dataGetErrorRecordList = mwsDataGetErrorRecordMapper.getListByTaskTime(taskEndTime);
//            if (null == dataGetErrorRecordList || dataGetErrorRecordList.size() == 0) {
//                isErrorReport = false;
//                log.info("***********本次定时任务全部数据以获取**********");
//            }
//            if (isErrorReportNum == isErrorReportMaxNum) {
//                log.info("已经重复获取了三次错误报告，是时候结束了！！！");
//                break;
//            }
//            try {
//                Thread.sleep(1800000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            for (MwsDataGetErrorRecord errorData : dataGetErrorRecordList) {
//                Shop shop = shopMapper.getOneInfoByName(errorData.getShop());
//                String errorTaskName = errorData.getName();
//                String taskArea = errorData.getArea();
//                int isRightSign = baseHandleByErrorTask(errorTaskName, shop, taskArea);
//                if (isRightSign == 0) {
//                    mwsDataGetErrorRecordMapper.updateIsHandle(errorData.getId());
//                }
//            }
//            List<MwsDataGetErrorRecord> dataGetErrorRecordResultList = mwsDataGetErrorRecordMapper.getListByTaskTime(taskEndTime);
//            if (null == dataGetErrorRecordResultList || dataGetErrorRecordResultList.size() == 0) {
//                log.info("***********获取----- ----错误任务重新成功，已拿到数据**********");
//            }
//        }
//    }
//

}
